package cn.tedu.serviceEdu.service.impl;

import cn.tedu.serviceEdu.client.VodClient;
import cn.tedu.serviceEdu.entity.EduVideo;
import cn.tedu.serviceEdu.mapper.EduVideoMapper;
import cn.tedu.serviceEdu.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author hong
 * @since 2023-05-18
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;

    @Override
    public void removeByCourseId(String id) {
        LambdaQueryWrapper<EduVideo> lqw = new LambdaQueryWrapper<>();
        lqw.select(EduVideo::getVideoSourceId);
        lqw.eq(EduVideo::getCourseId,id);
        List<EduVideo> videoList = this.list(lqw);

        List<String> videoIdList = videoList.stream()
                .filter(Objects::nonNull)
                .map(EduVideo::getVideoSourceId)
                .collect(Collectors.toList());
        System.out.println("批量删除视频videoIdList："+videoIdList);
        if (!videoIdList.isEmpty()){
            vodClient.deleteVideoBatch(videoIdList);
        }
        LambdaQueryWrapper<EduVideo>  queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduVideo::getCourseId,id);
         baseMapper.delete(queryWrapper);
    }


}
